/**
 * Notes: 注册登录模块业务逻辑 (新版API)
 * Ver : WeCollege Server API 1.0.0
 * Date: 2025-01-27
 * Description: 使用自建服务器API重构登录认证系统
 */

const BaseBiz = require('./base_biz.js');
const cacheHelper = require('../../helper/cache_helper.js');
const serverHelper = require('../../helper/server_helper.js');
const pageHelper = require('../../helper/page_helper.js');
const helper = require('../../helper/helper.js');
const constants = require('../constants.js');

class PassportBiz extends BaseBiz {

  // 静默登录(有登录状态则不登录)  
  static async loginSilence(that) {
    return await PassportBiz.loginCheck(false, 'slience', 'bar', that);
  }

  // 强制静默登录(不论是否有登录状态)  
  static async loginSilenceMust(that) {
    return await PassportBiz.loginCheck(false, 'must', 'bar', that);
  }

  // 必须登陆 可以取消(窗口形式) 
  static async loginMustCancelWin(that) {
    return await PassportBiz.loginCheck(true, 'cancel', '', that);
  }

  // 必须登陆 只能强制注册或者回上页(窗口形式)  
  static async loginMustBackWin(that) {
    return await PassportBiz.loginCheck(true, 'back', '', that);
  }

  // 获取token  
  static getToken() {
    let token = cacheHelper.get(constants.CACHE_TOKEN);
    return token || null;
  }

  // 设置token
  static setToken(token) {
    if (!token) return;
    cacheHelper.set(constants.CACHE_TOKEN, token, constants.CACHE_TOKEN_EXPIRE);
  }

  //  获取user id 
  static getUserId() {
    let token = cacheHelper.get(constants.CACHE_TOKEN);
    if (!token) return '';
    return token.id || '';
  }

  // 获取user name 
  static getUserName() {
    let token = cacheHelper.get(constants.CACHE_TOKEN);
    if (!token) return '';
    return token.name || '';
  }

  static getStatus() {
    let token = cacheHelper.get(constants.CACHE_TOKEN);
    if (!token) return -1;
    return token.status || -1;
  }

  // 是否登录 
  static isLogin() {
    let id = PassportBiz.getUserId();
    return (id.length > 0) ? true : false;
  }

  static loginStatusHandler(method, status) {
    let content = '';
    if (status == 0) content = '您的注册正在审核中，暂时无法使用此功能！';
    else if (status == 8) content = '您的注册审核未通过，暂时无法使用此功能；请在个人中心修改资料，再次提交审核！';
    else if (status == 9) content = '您的账号已经禁用, 无法使用此功能！';
    if (method == 'cancel') {
      wx.showModal({
        title: '温馨提示',
        content,
        confirmText: '取消',
        showCancel: false
      });
    }
    else if (method == 'back') {
      wx.showModal({
        title: '温馨提示',
        content,
        confirmText: '返回',
        showCancel: false,
        success(result) {
          wx.navigateBack();
        }
      });
    }
    return false;
  }

  // 登录判断及处理
  static async loginCheck(mustLogin = false, method = 'back', title = '', that = null) {
    let token = cacheHelper.get(constants.CACHE_TOKEN);
    if (token && method != 'must') {
      if (that)
        that.setData({
          isLogin: true
        });
      return true;
    } else {
      if (that) that.setData({
        isLogin: false
      });
    }

    let opt = {
      title: title || '登录中',
    };

    // 使用微信登录获取code
    let loginRes;
    try {
      loginRes = await wx.login();
      if (!loginRes.code) {
        throw new Error('获取微信登录code失败');
      }
    } catch (err) {
      console.error('微信登录失败:', err);
      if (mustLogin) {
        PassportBiz._showLoginError(method);
      }
      return false;
    }

    // 调用服务器登录接口
    let res = await serverHelper.callServerSumbit('user/login', {
      code: loginRes.code
    }, opt).then(result => {
      PassportBiz.clearToken();
      
      if (result && helper.isDefined(result.data.token) && result.data.token && result.data.token.status == 1) {
        PassportBiz.setToken(result.data.token);

        if (that) that.setData({
          isLogin: true
        });

        return true;
      }
      else if (mustLogin && result && helper.isDefined(result.data.token) && result.data.token && (result.data.token.status == 0 || result.data.token.status == 8 || result.data.token.status == 9)) {
        let status = result.data.token.status;
        return PassportBiz.loginStatusHandler(method, status);
      }
      else if (mustLogin) {
        return PassportBiz._showLoginError(method);
      }

      return false;
    }).catch(err => {
      console.log('登录失败:', err);
      PassportBiz.clearToken();
      
      if (mustLogin) {
        PassportBiz._showLoginError(method);
      }
      
      return false;
    });

    return res;
  }

  // 显示登录错误
  static _showLoginError(method) {
    if (method == 'cancel') {
      wx.showModal({
        title: '温馨提示',
        content: '此功能仅限注册用户',
        confirmText: '马上注册',
        cancelText: '取消',
        success(result) {
          if (result.confirm) {
            let url = pageHelper.fmtURLByPID('/pages/my/reg/my_reg') + '?retUrl=back';
            wx.navigateTo({ url });
          }
        }
      });
    } else if (method == 'back') {
      wx.showModal({
        title: '温馨提示',
        content: '此功能仅限注册用户',
        confirmText: '马上注册',
        cancelText: '返回',
        success(result) {
          if (result.confirm) {
            let retUrl = encodeURIComponent(pageHelper.getCurrentPageUrlWithArgs());
            let url = pageHelper.fmtURLByPID('/pages/my/reg/my_reg') + '?retUrl=' + retUrl;
            wx.redirectTo({ url });
          } else if (result.cancel) {
            let len = getCurrentPages().length;
            if (len == 1) {
              let url = pageHelper.fmtURLByPID('/pages/default/index/default_index');
              wx.reLaunch({ url });
            } else {
              wx.navigateBack();
            }
          }
        }
      });
    }
    return false;
  }

  // 清除登录缓存
  static clearToken() {
    cacheHelper.remove(constants.CACHE_TOKEN);
  }

  // 用户注册
  static async register(data) {
    // 先获取微信登录code
    let loginRes;
    try {
      loginRes = await wx.login();
      if (!loginRes.code) {
        throw new Error('获取微信登录code失败');
      }
    } catch (err) {
      throw new Error('微信登录失败');
    }

    // 构建注册数据
    const registerData = {
      code: loginRes.code,
      name: data.formName,
      mobile: data.formMobile,
      forms: data.formForms || []
    };

    // 调用注册接口
    const result = await serverHelper.callServerSumbit('user/register', registerData);
    
    if (result && result.data && result.data.token) {
      PassportBiz.setToken(result.data.token);
      return result;
    }
    
    throw new Error(result.message || '注册失败');
  }

  // 获取用户详情
  static async getMyDetail() {
    return await serverHelper.callServerData('user/profile', {});
  }

  // 编辑用户基本信息
  static async editBase(data) {
    const updateData = {
      name: data.formName,
      mobile: data.formMobile,
      forms: data.formForms || []
    };

    return await serverHelper.callServerSumbit('user/updateProfile', updateData);
  }

  // 手机号码获取 (使用新的微信API)
  static async getPhone(e, that) {
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      try {
        // 使用新的手机号获取方式
        const code = e.detail.code;
        if (!code) {
          throw new Error('获取手机号授权码失败');
        }

        let params = { code };
        let opt = { title: '手机验证中' };
        
        await serverHelper.callServerSumbit('user/getPhone', params, opt).then(res => {
          let phone = res.data.phone || res.data;
          if (!phone || phone.length < 11) {
            wx.showToast({
              title: '手机号码获取失败，请重新填写手机号码',
              icon: 'none',
              duration: 2000
            });
          } else {
            that.setData({
              formMobile: phone
            });
          }
        });
      } catch (err) {
        console.error('获取手机号失败:', err);
        wx.showToast({
          title: '手机号码获取失败，请重新填写手机号码',
          icon: 'none'
        });
      }
    } else {
      wx.showToast({
        title: '手机号码获取失败，请重新填写手机号码',
        icon: 'none'
      });
    }
  }

  // 微信登录 (获取用户信息)
  static async getWechatUserInfo() {
    return new Promise((resolve, reject) => {
      wx.getUserProfile({
        desc: '用于完善用户资料',
        success: (res) => {
          resolve(res.userInfo);
        },
        fail: (err) => {
          console.error('获取用户信息失败:', err);
          reject(err);
        }
      });
    });
  }

  // 完整的微信登录流程
  static async wechatLogin() {
    try {
      // 1. 获取微信登录code
      const loginRes = await wx.login();
      if (!loginRes.code) {
        throw new Error('获取微信登录code失败');
      }

      // 2. 获取用户信息 (可选)
      let userInfo = null;
      try {
        userInfo = await PassportBiz.getWechatUserInfo();
      } catch (err) {
        console.log('用户取消授权或获取用户信息失败');
      }

      // 3. 调用服务器登录接口
      const loginData = {
        code: loginRes.code,
        userInfo: userInfo
      };

      const result = await serverHelper.callServerSumbit('user/login', loginData, {
        title: '登录中...'
      });

      if (result && result.data && result.data.token) {
        PassportBiz.setToken(result.data.token);
        return {
          success: true,
          token: result.data.token,
          user: result.data.user
        };
      }

      throw new Error(result.message || '登录失败');
    } catch (err) {
      console.error('微信登录失败:', err);
      return {
        success: false,
        error: err.message || '登录失败'
      };
    }
  }
}

/** 表单校验 */
PassportBiz.CHECK_FORM = {
  name: 'formName|must|string|min:1|max:30|name=昵称',
  mobile: 'formMobile|must|len:11|name=手机',
  forms: 'formForms|array'
};

module.exports = PassportBiz;